package com.jscq.yygh.user.controller;

import com.alibaba.excel.util.StringUtils;
import com.atguigu.yygh.common.result.Result;
import com.atguigu.yygh.model.hosp.Order;
import com.atguigu.yygh.model.user.UserInfo;
import com.atguigu.yygh.vo.hosp.OrderSearchVo;
import com.atguigu.yygh.vo.hosp.OrderVo;
import com.atguigu.yygh.vo.user.UserInfoQueryVo;
import com.atguigu.yygh.vo.user.UserInfoVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jscq.yygh.user.service.OrderService;
import com.jscq.yygh.user.service.UserInfoService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.Map;

@RestController
@RequestMapping("/admin/user")
public class UserController {
    //后台使用controller
    @Autowired
    private UserInfoService userInfoService;


    @Autowired
    private OrderService orderService;

    // 添加user
    @ApiOperation(value = "添加user")
    @PostMapping("addUser")
    public Result addUser(@PathVariable String stuCode,@PathVariable String name){
        UserInfo  userInfo = new UserInfo();
        userInfo.setStatus(1);
        userInfo.setStuCode(stuCode);
        userInfo.setCertificatesType("学生证");
        userInfo.setIsDeleted(0);
        UserInfo userInfoExist = userInfoService.selectUserInfoByStuCode(stuCode);

        return Result.ok();
    }

    // 逻辑删除
    @ApiOperation(value = "逻辑删除订单信息")
    @DeleteMapping("deleteOrder/{id}")
    public Result deleteOrder(@PathVariable Long id){
        boolean flag = orderService.removeById(id);
        if (flag){
            return Result.ok();
        }else {
            return Result.fail();
        }

    }

    //更新订单状态
    @ApiOperation(value = "更新宿舍上线状态")
    @GetMapping("/updateOrderStatus/{id}/{status}")
    public Result updateOrderStatus(@PathVariable String id,
                                    @PathVariable Integer status){
        Order order = orderService.getById(id);
        order.setStatus(status);
        orderService.updateById(order);
        return Result.ok();
    }

    @PostMapping("addOrder")
    public Result addOrder(@RequestBody OrderVo orderVo){
        Order order = new Order();
        System.out.println("orderVoTime:"+orderVo.getDate());
        BeanUtils.copyProperties(orderVo,order);
        order.setStatus(0);
        order.setCreateTime(new Date());
        order.setUpdateTime(new Date());
        order.setIsDeleted(0);
        System.out.println("order:"+order.toString());
        orderService.save(order);
        return Result.ok();
    }


    //条件查询带分页
    @GetMapping("getOrderByStuCode/{current}/{limit}")
    public Result getOrderByStuCode(@PathVariable long current,
                                    @PathVariable long limit,
                                    OrderSearchVo orderVo){
        //创建page对象,传递当前页与记录数
        Page<Order> page=new Page<>(current,limit);

        //调用方法实现分页查询

        //Page<Order> orderPage = orderService.page(page,wrapper);
        IPage<Order> pageModel = orderService.selectPage(page,orderVo);
        return Result.ok(pageModel);
    }

    //用户列表(条件查询带分页)
    @GetMapping("{page}/{limit}")
    public Result list(@PathVariable int page, @PathVariable int limit, UserInfoQueryVo userInfoQueryVo){
        System.out.println("管理员管理用户");
        Page<UserInfo> pageParam = new Page<>(page,limit);
        IPage<UserInfo> pageModel = userInfoService.selectPage(pageParam,userInfoQueryVo);
        return Result.ok(pageModel);
    }

    //认证审批
    @GetMapping("approval/{userId}/{authStatus}")
    public Result approval(@PathVariable Long userId,@PathVariable Integer authStatus) {

        userInfoService.approval(userId,authStatus);
        return Result.ok();
    }

    //用户锁定
    @GetMapping("lock/{userId}/{status}")
    public Result lock(@PathVariable Long userId,@PathVariable Integer status){
        userInfoService.lock(userId,status);
        return Result.ok();
    }

    //用户详情
    @GetMapping("show/{userId}")
    public Result show(@PathVariable Long userId) {
        Map<String,Object> map = userInfoService.show(userId);
        return Result.ok(map);
    }

}
